package com.hongyun.tms.web.controller;


import com.hongyun.tms.cache.CacheManager;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.dto.dict.DictItemResponseDTO;
import com.hongyun.tms.common.dto.dict.MDictDataDTO;
import com.hongyun.tms.common.dto.dict.MDictTypeDTO;
import com.hongyun.tms.common.dto.dict.MDictTypeSearchDTO;
import com.hongyun.tms.common.page.PageParameter;
import com.hongyun.tms.common.po.MDictTypePO;
import com.hongyun.tms.domain.AjaxResult;
import com.hongyun.tms.service.MDictService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 孙琦
 * @Description 字典及字典类型操作controller类
 * @date 2023-4-26 16:57:35
 */
@RestController
@RequestMapping(value = "/basicData/dict")
@Validated
@Slf4j
@Api(tags = "基础数据.字典管理")
public class MDictController {
    @Autowired
    private CacheManager cacheManager;
    /**
     * 日志组件
     **/

    @Autowired
    private MDictService dictService;

    /**
     * 新增字典类型
     *
     * @param po 字典类型实体类
     * @return 响应数据
     */
    @PostMapping("/insertDictType")
//    @PreAuthorize("hasAuthority('master:dict:insert')")
//	@Log(OperateTypeEnum.INSERT)
    public AjaxResult insertDictType(@RequestBody MDictTypeDTO po) {
        dictService.insertOrUpdateDictType(po);

        return AjaxResult.success();
    }


    /**
     * 根据id查询字典类型
     *
     * @param id 字典实体类
     * @return 响应数据
     */
    @GetMapping("/getDictTypeById/{id}")
//    @PreAuthorize("hasAuthority('master:dict:query')")
//	@Log(OperateTypeEnum.QUERY)
    public AjaxResult getDictTypeById(@PathVariable("id") Long id) {
        MDictTypePO dictTypePO = dictService.getDictTypeById(id);
        return AjaxResult.success(dictTypePO);
    }


    /**
     * 根据ID删除字典类型
     *
     * @param id 字典类型实体类
     * @return 响应数据
     */
    @DeleteMapping("/deleteDictTypeById/{id}")
//    @PreAuthorize("hasAuthority('master:dict:delete')")
//	@Log(OperateTypeEnum.DELETE)
    public AjaxResult deleteDictTypeById(@PathVariable("id") Long id) {
        dictService.deleteDictTypeById(id);
        return AjaxResult.success();
    }

    /**
     * 修改字典类型
     *
     * @param po 字典类型实体类
     * @return 响应数据
     */
    @PutMapping("/updateDictType")
//    @PreAuthorize("hasAuthority('master:dict:update')")
//	@Log(OperateTypeEnum.UPDATE)
    public AjaxResult updateDictType(@RequestBody MDictTypeDTO po) {
        dictService.insertOrUpdateDictType(po);
        return AjaxResult.success();
    }


    /**
     * 查询字典类型列表
     *
     * @param mDictTypeSearchDTO
     * @return 响应数据
     */
    @GetMapping("/getAllDictTypeList")
//    @PreAuthorize("hasAuthority('master:dict:query')")
//	@Log(OperateTypeEnum.QUERY)
    public AjaxResult<PageResponseDto<MDictTypeDTO>> getDictTypeList(MDictTypeSearchDTO mDictTypeSearchDTO) {
        PageResponseDto<MDictTypeDTO> list = dictService.getDictTypeList(mDictTypeSearchDTO);
        return AjaxResult.success(list);
    }


//字典类型操作↑
//字典操作↓

    /**
     * 查询字典列表
     *
     * @param po 字典实体类
     * @return 响应数据
     */
    @GetMapping("/getDictList")
    public AjaxResult getDictList(PageParameter pageParameter, MDictDataDTO po) {
        PageResponseDto<MDictDataDTO> list = dictService.getDictList(pageParameter, po);
        return AjaxResult.success(list);
    }

    /**
     * 根据type查询对应字典项列表
     *
     * @param dictType
     * @return
     */
    @GetMapping("/dictItemListByType")
    public AjaxResult dictItemListByType(@RequestParam(value = "dictType", required = false) String dictType) {
        List<DictItemResponseDTO> list = dictService.dictItemListByType(dictType);
        return AjaxResult.success(list);
    }

    /**
     * 新增字典
     *
     * @param po 字典类型实体类
     * @return 响应数据
     */
    @PostMapping("/insertDict")
//    @PreAuthorize("hasAuthority('master:dict:insert')")
//	@Log(OperateTypeEnum.INSERT)
    public AjaxResult insertDict(@RequestBody MDictDataDTO po) {
        dictService.insertOrUpdateDict(po);
        return AjaxResult.success();
    }

    /**
     * 修改字典
     *
     * @param po 字典类型实体类
     * @return 响应数据
     */
    @PutMapping("/updateDict")
//    @PreAuthorize("hasAuthority('master:dict:update')")
//	@Log(OperateTypeEnum.UPDATE)
    public AjaxResult updateDict(@RequestBody MDictDataDTO po) {
        dictService.insertOrUpdateDict(po);
        return AjaxResult.success();
    }

    /**
     * 根据类型查询字典列表
     *
     * @param typeCd 字典类型
     * @return 响应数据
     */
    @GetMapping("/getDictListByType")
//    @PreAuthorize("hasAuthority('master:dict:query')")
//	@Log(OperateTypeEnum.QUERY)
    public AjaxResult getDictListByType(PageParameter pageParameter, String typeCd) {
        PageResponseDto<MDictDataDTO> list = dictService.getDictListByType(pageParameter, typeCd);
        return AjaxResult.success(list);
    }

    /**
     * 根据ID删除字典
     *
     * @param id 字典类型实体类
     * @return 响应数据
     */
    @DeleteMapping("/deleteDictById/{id}")
//    @PreAuthorize("hasAuthority('master:dict:delete')")
//	@Log(OperateTypeEnum.DELETE)
    public AjaxResult deleteDictById(@PathVariable("id") Long id) {
        dictService.deleteDictById(id);
        return AjaxResult.success();
    }


    /**
     * 根据id查询字典
     *
     * @param id 字典实体类
     * @return 响应数据
     */
    @GetMapping("/getDictById/{id}")
//    @PreAuthorize("hasAuthority('master:dict:query')")
//	@Log(OperateTypeEnum.QUERY)
    public AjaxResult getDictById(@PathVariable("id") Long id) {

        MDictDataDTO list = dictService.getDictById(id);
        return AjaxResult.success(list);
    }

    /**
     * 刷新redis中的字典缓存
     *
     * @return result
     */
    @GetMapping("refreshCache")
    public AjaxResult refreshCache() {
        try {
            cacheManager.refreshCache();
        } catch (Exception e) {
            log.error("刷新字典缓存失败", e);
            return AjaxResult.fail("刷新失败，请检查缓存服务器");
        }
        return AjaxResult.success();
    }
}
